define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wdgroup/wdocument/index',
                    add_url: 'wdgroup/wdocument/add',
                    edit_url: 'wdgroup/wdocument/edit',
                    del_url: 'wdgroup/wdocument/del',
                    multi_url: 'wdgroup/wdocument/multi',
                    table: 'wdocument',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [{checkbox: true},
                    {field: 'id', title: __('Id'), operate:false },
                    {field: 'title', title: __('标题')},
                    {field: 'categorys', title: __('所属分类'), operate:false, formatter: Table.api.formatter.label},
                    {field: 'flag', title: __('旗子数'),operate:false},
                    {field: 'likes', title: __('点赞数'),operate:false},
                    {field: 'once_clicks', title: __('当日浏览量'), operate:false},
                    {field: 'is_audit', title: __('审核'), searchList: {"0":__('未审核'),"1":__('已审核')}, formatter: function(value, row, index) {
                        var colorMap = {"0": "danger", "1": "success"};
                        var textMap = {"0": __('未审核'), "1": __('已审核')};
                        return '<span class="text-' + colorMap[value] + '">' + textMap[value] + '</span>';
                    }},
                    {field: 'status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
                    {field: 'user.nickname', operate: false,title: __('创建者')},
                    {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($('form[role=form]'));
            }
        }
    };
    return Controller;
});